//
//  PublishMessageViewController.m
//  BigToBig
//
//  Created by yi liu on 16/8/8.
//  Copyright © 2016年 cn.cloudtp. All rights reserved.
//

#import "PublishMessageViewController.h"
#import "IncubationSpacePrincipleCell.h"
#import "PublishMessageRentTypeCell.h"

@interface PublishMessageViewController ()
<
    UITableViewDelegate,
    UITableViewDataSource,
    PublishMessageRentTypeCellDelegate
>
@property (nonatomic , strong)UITableView *mainTableView;

@property (nonatomic , strong)NSArray *sectionArr;
@property (nonatomic , strong)NSArray *infoArr;
@property (nonatomic , strong)NSMutableArray *rentArr;
@property (nonatomic , strong)NSArray *rentHeadArr;
@property (nonatomic , strong)NSArray *rentType1Arr;
@property (nonatomic , strong)NSArray *rentType2Arr;
@property (nonatomic , strong)NSArray *imageArr;
@property (nonatomic , strong)NSMutableDictionary *dataDic;

@property (nonatomic , assign)NSInteger Selected;

@end

@implementation PublishMessageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
    self.navigationController.navigationBar.barTintColor = COLOR_RGBA(77, 155, 255, 1);
    [self createData];
    UILabel *titleLab = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 40)];
    titleLab.text = @"发布消息";
    titleLab.textColor = [UIColor whiteColor];
    [self setBackBar];
    titleLab.textAlignment = NSTextAlignmentCenter;
    self.navigationItem.titleView = titleLab;
    [self createView];

}
#pragma mark 创建数据
- (void)createData{
    self.sectionArr = [[NSArray alloc] initWithObjects:@"1",@"2",@"3", nil];
    
    NSDictionary *dic00 = [[NSDictionary alloc] initWithObjectsAndKeys:@"联系方式",@"name",@"请填写",@"content", nil];
    NSDictionary *dic01 = [[NSDictionary alloc] initWithObjectsAndKeys:@"地址",@"name",@"请填写",@"content", nil];
    NSDictionary *dic02 = [[NSDictionary alloc] initWithObjectsAndKeys:@"行业描述",@"name",@"请填写",@"content", nil];
    NSDictionary *dic03 = [[NSDictionary alloc] initWithObjectsAndKeys:@"资源名称",@"name",@"请填写",@"content", nil];
    NSDictionary *dic04 = [[NSDictionary alloc] initWithObjectsAndKeys:@"资源描述",@"name",@"请简单的描述一下",@"content", nil];
    NSDictionary *dic05 = [[NSDictionary alloc] initWithObjectsAndKeys:@"服务特色",@"name",@"请填写",@"content", nil];
    self.infoArr = [[NSArray alloc] initWithObjects:dic00,dic01,dic02,dic03,dic04,dic05, nil];
    NSDictionary *dic1 = [NSDictionary dictionary];
    self.rentHeadArr = [[NSArray alloc] initWithObjects:dic1,nil];
    self.rentArr = [[NSMutableArray alloc]initWithObjects:dic1, nil];
    NSDictionary *dic110 = [[NSDictionary alloc] initWithObjectsAndKeys:@"出租面积",@"name",@"请填写",@"content", nil];
    
    NSDictionary *dic111 = [[NSDictionary alloc] initWithObjectsAndKeys:@"出租价钱",@"name",@"元/平米/周",@"content", nil];
    NSDictionary *dic112 = [[NSDictionary alloc] initWithObjectsAndKeys:@"租金描述",@"name",@"请填写",@"content", nil];
    self.rentType1Arr = @[dic110,dic111,dic112];
    
    NSDictionary *dic120 = [[NSDictionary alloc] initWithObjectsAndKeys:@"出租工位",@"name",@"请填写",@"content", nil];
    
    NSDictionary *dic121 = [[NSDictionary alloc] initWithObjectsAndKeys:@"出租价钱",@"name",@"元/工位/周",@"content", nil];
    NSDictionary *dic122 = [[NSDictionary alloc] initWithObjectsAndKeys:@"租金描述",@"name",@"请填写",@"content", nil];
    
    self.rentType2Arr = @[dic120,dic121,dic122];
    
    NSDictionary *dic20 = [[NSDictionary alloc] initWithObjectsAndKeys:@"资源照片",@"name",@"点击上传",@"content", nil];
    self.imageArr = [[NSArray alloc] initWithObjects:dic20, nil];
    
    _dataDic = [[NSMutableDictionary alloc]initWithObjectsAndKeys:_infoArr,[_sectionArr objectAtIndex:0],_rentArr,[_sectionArr objectAtIndex:1],_imageArr,[_sectionArr objectAtIndex:2],nil];

}

#pragma mark 导航栏左右Button
- (void)setBackBar{
    
    BackButton *backBtn=[BackButton buttonWithType:UIButtonTypeCustom];
    backBtn.frame = CGRectMake(0, 0, 44, 44);
    [backBtn addTarget:self action:@selector(backController:) forControlEvents:UIControlEventTouchUpInside];
    [backBtn setImage:[UIImage imageNamed:@"返回"] forState:UIControlStateNormal];
    [backBtn.imageView setContentMode:UIViewContentModeLeft];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:backBtn];
    [self.navigationItem setHidesBackButton:YES animated:YES];
    
    BackButton *saveBtn=[BackButton buttonWithType:UIButtonTypeCustom];
    saveBtn.frame = CGRectMake(kScreenWidth - 44,0 , 44, 44);
    [saveBtn addTarget:self action:@selector(SaveMessage) forControlEvents:UIControlEventTouchUpInside];
    [saveBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [saveBtn setTitle:@"提交" forState:UIControlStateNormal];
    [saveBtn.imageView setContentMode:UIViewContentModeLeft];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:saveBtn];
    [self.navigationItem setHidesBackButton:YES animated:YES];
    
}

- (void)backController:(UIButton *)button{
    self.tabBarController.tabBar.hidden = NO;
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark 提交
- (void)SaveMessage{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"继续完善内容" message:nil preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        [self.navigationController dismissViewControllerAnimated:alert completion:nil];
    }];
    UIAlertAction *ok = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        ProjectInfoViewController *pro = [ProjectInfoViewController alloc];
        [self.navigationController pushViewController:pro animated:YES];
    }];
    
    [alert addAction:cancel];
    [alert addAction:ok];
    [self.navigationController
     presentViewController:alert animated:YES completion:nil];
    
}
#pragma mark - 创建View
- (void)createView{
     // 主tableView
    self.mainTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight-49) style:UITableViewStyleGrouped];

    [self.view addSubview:_mainTableView];
    self.mainTableView.delegate = self;
    self.mainTableView.dataSource = self;

    self.mainTableView.sectionHeaderHeight = 40;
    self.mainTableView.sectionFooterHeight = 0;
    self.mainTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.mainTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:NSStringFromClass([UITableViewCell class])];
    [self.mainTableView registerClass:[IncubationSpacePrincipleCell class] forCellReuseIdentifier:NSStringFromClass([IncubationSpacePrincipleCell class])];
    
}
#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return _sectionArr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return _infoArr.count;
    }if (section == 1) {
        return _rentArr.count;
    }if (section == 2) {
        return _imageArr.count;
    }
    return 0;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0 || section == 1|| section == 2) {
        return 15;
    }
    return 0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    if (section == 0 || section == 1) {
        return 0.001f;
    }
    return 60;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 70;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *str = [_sectionArr objectAtIndex:indexPath.section];
    NSArray *arr = [_dataDic objectForKey:str];
    if (indexPath.section == 1 && indexPath.row == 0) {
        NSString *CellIdentifier = [NSString stringWithFormat:@"cell%ld%ld",indexPath.section,indexPath.row];

        PublishMessageRentTypeCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

        cell = [[PublishMessageRentTypeCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
        cell.isSelected = self.Selected;
        cell.textLabel.text = @"出租方式";
        cell.textLabel.font = [UIFont systemFontOfSize:15];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.publishRentTypeCellDelegate = self;
        return cell;
    }
    else{
        IncubationSpacePrincipleCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([IncubationSpacePrincipleCell class]) forIndexPath:indexPath];

        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.principleKeyLabel.text = [[arr objectAtIndex:indexPath.row] valueForKey:@"name"];
        cell.principleValueLabel.text = [[arr objectAtIndex:indexPath.row] valueForKey:@"content"];
        
        return cell;
    
    }
}
- (void)changeSelectedWithSelected:(NSInteger)selected{
    self.Selected = selected;
    [self.rentArr removeAllObjects];
    
    if (selected == 1000) {
        [_rentArr addObjectsFromArray:self.rentHeadArr];
        [_rentArr addObjectsFromArray:self.rentType1Arr];
    }
    else if (selected == 1001){
        [_rentArr addObjectsFromArray:self.rentHeadArr];
        [_rentArr addObjectsFromArray:self.rentType2Arr];
    }
    [self.mainTableView reloadData];
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    
    UIView *view = [[UIView alloc] init];
    view.backgroundColor = [UIColor colorWithWhite:0.949 alpha:1.000];
    UILabel *titleLabel = [[UILabel alloc] init];
    titleLabel.textColor = [UIColor grayColor];
    titleLabel.numberOfLines = 0;
    titleLabel.font = [UIFont systemFontOfSize:14];
    [view addSubview:titleLabel];
    if (section == 2) {
        view.frame = CGRectMake(0, 0, kScreenWidth, 50);
        titleLabel.frame = CGRectMake(10, 0, kScreenWidth - 20, 60);
        titleLabel.text = @"建议上传尺寸为1400*600的图片请上传资源照片.最多上传五张图片, 上传图片格式为(JPG. PNG. PDF). 上传文件小于5MB.";
    }
    
    return view;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UILabel *headerLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 10)];
    return headerLabel;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
